﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using BidFunLib.Services;
using System.Windows.Forms;
using BidFunLib.Entity;
using BidFunLib.BidWeb.Sale;
using BidFunLib.BidWeb;
using BidFunLib;

namespace BidFunSpy.BidWeb
{

    public class BidWebRootSpy : BidWebRootEngine<BidWebSale>
    {
        public BidWebRootSpy() : base()
        {
            SaleWebManager = new BidSaleWebManager();
        }

        /// <summary>
        /// New Sale Detected on main form
        /// </summary>
        /// <param name="bid_url">URL</param>
        /// <param name="objectLabel">Object Label</param>
        /// <param name="robot">Robot ?</param>
        /// <param name="beginnerOnly">Beginners Only ?</param>
        /// <param name="bidNumber">Bid Number in html document</param>
        public override void newSaleDetected(string bid_url, string objectLabel, bool robot, bool beginnerOnly, int bidNumber)
        {
            if (AllSales == false)
            {
                foreach (BidObjectSpy auto in SelectedBidObjectForSpy)
                {
                    if (auto.Spy == true && auto.Name == objectLabel)
                    {
                        if (SaleWebManager.CheckForSale(bid_url) == false)
                        {
                            BidWebSaleEngine newBid = SaleWebManager.createBidSaleWeb(bid_url, objectLabel, robot, beginnerOnly);
                            newBid.bidEndedEvent += new OnBidEndedHandler(SaleManager_bidEndedEvent);
                            newBid.StatsHander += new OnStatsHander(newBid_StatsHander);
                            break;
                        }
                    }
                }
            }
            else
            {
                if (SaleWebManager.CheckForSale(bid_url) == false)
                {
                    BidWebSaleEngine newBid = SaleWebManager.createBidSaleWeb(bid_url, objectLabel, robot, beginnerOnly);
                    newBid.bidEndedEvent += new OnBidEndedHandler(SaleManager_bidEndedEvent);
                    newBid.StatsHander += new OnStatsHander(newBid_StatsHander);
                }
            }
        }

        /// <summary>
        /// Start bid and clean main web browser
        /// </summary>
        public override void postBidParsing()
        {
            SaleWebManager.StartBids();
            Browser.Dispose();
            Browser.DocumentCompleted -= Browser_DocumentCompleted;
            Browser = null; 
        }
    }

    /// <summary>
    /// Map for BidSaleWeb
    /// </summary>
    public class BidSaleWebManager : BidSaleWebEngineManager<BidWebSale>
    {
        protected override BidWebSale createBid(string url)
        {
            return new BidWebSale(url);
        }
    }
}
